package paper.xml;

import java.io.*;
import java.util.*;

import paper.xml.SurroundStream;

class Paper 
{
    String name;
    String[] authors = new String[0];
    String where;
    int id;
    int date = -1;
    int year = -1;
    int stPage=-1,endPage=-1;
    int volume = -1;
    double  score;
    int[] cites,citedBy;
    int   numCites,numCitedBy,numAuthors;
}

class Person
{
    String name;
    double  score;
}

class Comment 
{
    String body;
    int author;
    int topic;
    int targetType;     // 0 = Comment  1 = Paper
    double  score;
}


class PaperSetComplete implements PaperSet {
    Vector<Paper> papers;
    Paper  current;
    HashMap<String,Integer> authors;
    int numParsed=0;

    PaperSetComplete()
    {
        papers = new Vector<Paper>();
        current = new Paper();
        authors = new HashMap<String,Integer>();
    }
    
    public void setCurrentId(int x) {
        current.id = x;
    }
    
    public void setCurrentName(String x) {
        current.name = x;
    }

    public void setCurrentStPage(int x) {
        current.stPage = x;
    }

    public void setCurrentEndPage(int x) {
        current.endPage = x;
    }

    public void setCurrentYear(int x) {
        current.year = x;
    }

    public void setCurrentVolume(int x) {
        current.volume = x;
    }

    public void setCurrentDate(int x) {
        current.date = x;
    }

    public void setCurrentWhere(String x) {
        current.where = x;
    }

    public void finalise(Vector<Integer> cites, Vector<Integer> citedBy,
                         Vector<String> auths)
    {
        current.numCites = cites.size();
        if( current.numCites>0 )
        {
            current.cites = new int[cites.size()];
            int i;
            for(i=0; i<cites.size(); i++)
                current.cites[i] = ((Integer)cites.elementAt(i)).intValue();
        }
        current.numCitedBy = citedBy.size();
        if( current.numCitedBy>0 )
        {
            current.citedBy = new int[citedBy.size()];
            int i;
            for(i=0; i<citedBy.size(); i++)
                current.citedBy[i] = ((Integer)citedBy.elementAt(i)).intValue();
        }
        current.numAuthors = auths.size();
        if( current.numAuthors>0 )
        {
            current.authors = new String[auths.size()];
            int i;
            for(i=0; i<auths.size(); i++)
                current.authors[i] = (String)auths.elementAt(i);
        }

        if( numParsed++ % 10000 == 0)
            System.out.println("Parsed " + numParsed);
        
        hashDetails();
    //    output( current );
        current = new Paper();
    }

    void hashDetails()
    {
        for(int i=0; i<current.authors.length; i++)
        {
            Integer v = authors.get( current.authors[i] );
            if( v == null )
                v = new Integer(0);
            v = new Integer( v.intValue()+1 );
            authors.put( current.authors[i], v);
            //authors.add( current.authors[i] );
        }
    }

    void dumpHash()
    {
        Set<String> ks = authors.keySet();
        Iterator<String> it = ks.iterator();
        while( it.hasNext() )
        {
            String name = it.next();
            Integer num = authors.get(name);
            System.out.println( name + " : " + num );
        }
    }

    void output(Paper p)
    {
        System.out.print("TIT=" + '"' + p.name + '"' + " AUTHS=[");
        for(int j=0; j<p.authors.length; j++)
            System.out.print(p.authors[j] + ",");
        System.out.print("] DATE=" + p.date);
        if( p.year != -1)
            System.out.print(" YEAR=" + p.year);
        if( p.stPage != -1)
            System.out.print(" PAGES=" + p.stPage + "," + p.endPage);
        if( p.volume != -1)
            System.out.print(" VOLUME=" + p.volume);
        if( p.where != null)
            System.out.print(" WHERE=\"" + p.where + "\"");
        System.out.println("");
    }
}
